// Ray Bai
// C++ Code: Find area of a circle, rectangle, or triangle
// Geometry Calculator
// 5/18/2011

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
int selection; // Menu selection
const double PI = 3.14159; // Set PI as constant
double radius, areaCircle, // For area of a circle
length, width, areaRectangle, // For area of a rectangle
base, height, areaTriangle; // For area of a triangle

do {
// Display the menu and allow user to choose 1-4.
cout << "\n\t\tGeometry Calculator\n\n";
cout << "\t1. Calculate the Area of a Circle\n";
cout << "\t2. Calculate the Area of a Rectangle\n";
cout << "\t3. Calculate the Area of a Triangle\n";
cout << "\t4. Quit\n\n";
cout << "\tEnter your choice (1-4): ";
cin >> selection;
cout << fixed << showpoint << setprecision(4);

// Validate menu selection.
if (selection < 1 || selection > 4)
{
cout << endl;
cout << "\t**Error: Menu option must be 1, 2, 3, or 4.\n\n";
cout << "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}

// Respond to user's menu selection.
switch (selection)
{
case 1: // Find area of a circle
cout << endl;
cout << "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << "\t\tArea of a Circle\n\n";
cout << "\tEnter radius: ";
cin >> radius;
if (radius < 0)
{
cout << endl;
cout << "\t**Error: Radius cannot be negative.\n\n";
cout << "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}
else
{
areaCircle = PI * pow(radius, 2.0);
cout << "\tArea = " << areaCircle << " square units";
cout << "\n\n\t~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}
break;

case 2: // Find area of a rectangle
cout << endl;
cout << "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << "\t\tArea of a Rectangle\n\n";
cout << "\tEnter length: ";
cin >> length;
cout << "\tEnter width: ";
cin >> width;
if (length <= 0 || width <= 0)
{
cout << endl;
cout << "\t**Error: Length and width must be positive ";
cout << "values.\n\n";
cout << "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}
else
{
areaRectangle = length * width;
cout << "\tArea = " << areaRectangle << " square units";
cout << "\n\n\t~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}
break;

case 3: // Find area of a triangle
cout << endl;
cout << "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << "\t\tArea of a Triangle\n\n";
cout << "\tEnter base: ";
cin >> base;
cout << "\tEnter height: ";
cin >> height;
if (base <= 0 || height <= 0)
{
cout << endl;
cout << "\t**Error: Base and height must be positive ";
cout << "values.\n\n";
cout << "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}
else
{
areaTriangle = base * height * 0.5;
cout << "\tArea = " << areaTriangle << " square units";
}

case 4: // Quit the program
cout << "\n\n\tThank you for using ";
cout << "the Geometry Calculator.\n\n";
}
} while(selection != 4);

system("pause");
return 0;
}

